var catalogId = GetQueryString("catalogId");
var costId = GetQueryString("costId");
var type = GetQueryString("type");
var allNode ;
var treeObj ;

var setting = {
	view: {
		showLine: false
	},
	data: {
		simpleData: {
			enable: true
		}
	}
};

$("#prevBtn").on("click",function (){
    window.location.href = "/ceca_two/modules/quota/civil/one.html";
});

$("#nextBtn").on("click",function (){
	var nowUrl = window.location.href;
    saveDate();
    if (nowUrl.indexOf("industry") !== -1) {
        window.location.href = "/ceca_two/modules/share/shareSubmit.html?costId="+costId+"&catalogId="+catalogId+"&type="+type;
    }
    if (nowUrl.indexOf("civil") !== -1){
        window.location.href = "/ceca_two/modules/share/shareSubmit.html?costId="+costId+"&catalogId="+catalogId+"&type="+type;
    }
    if (nowUrl.indexOf("civicism") !== -1){
        window.location.href = "/ceca_two/modules/share/shareSubmit.html?costId="+costId+"&catalogId="+catalogId+"&type="+type;
    }
});

drawTable(catalogId , type);

function drawTable(catalogId,type){
    $.ajax({
		type:"post",
		url:"/guidance_service/front/costIndex/template/getByCatalogId.do",
		data:{
            catalogId:catalogId,
			type : type
		},success : function (result){
			if (result.resultCode == 200){
                var data = JSON.parse(result.data.templateContent)[0];
                allNode = data;
                console.log(data);

				var nowUrl = window.location.href;
                var length;
                if (nowUrl.indexOf("industry") != -1 && sessionStorage.getItem("industryTwo")) {
                    length = JSON.parse(sessionStorage.getItem("industryTwo")).length;
                    drawTr(data , length);
                }else if (nowUrl.indexOf("civil") != -1 && sessionStorage.getItem("civilTwo")){
                    length = JSON.parse(sessionStorage.getItem("civilTwo")).length;
                    drawTr(data , length);
                }else if (nowUrl.indexOf("civicism") != -1 && sessionStorage.getItem("civicismTwo")){
                    length = JSON.parse(sessionStorage.getItem("civicismTwo")).length;
                    drawTr(data , length);
                }else{
                    drawTr(data , 5)
				}

                getDate();

				var t = $.fn.zTree.init($("#treeDemo"), setting, data);
				treeObj = $.fn.zTree.getZTreeObj("treeDemo");

			}else{
                layer.msg("请求错误，请稍后重试",{icon:5,time:2000});
			}
		},error : function (){
			layer.msg("请求错误，请稍后重试",{icon:5,time:2000});
		}
	});
}

function drawTr(json , length) {

	for( i = 0 ; i < length ; i++){
		var html = "<tr><td>"+(i+1)+"</td><td class='selectArrow' onclick ='showList(this,0)' data-id = "+
            json.id+"><p></p><div  onclick='hideList(this)' class='listBox clearfloat'></div></td><td><input type='text' class='inp'/></td><td><input type='text' class='inp'/></td><td><input type='text' class='inp'/></td><td><input type='text' class='inp'/></td><td><input type='text' class='inp'/></td><td></td><td class='delete' onclick='deleteChoose(this)'><div class=\"oper\"><img src=\"/ceca_two/img/wang2.png\" class=\"del1\" onclick=\"del(this)\"><span style=\"flaot:left\">删除</span></div></td></tr>";
		$("#mainBox").append(html);
	}
}


function showList(modern,level){
    var thisId = $(modern).attr("data-id");
    var thisNode = treeObj.getNodeByParam("id",thisId);	
	$(".listbox").show();
	creatList(modern,thisNode.children ,level);
	
	window.event? window.event.cancelBubble = true : e.stopPropagation();
}


function deleteChoose(modern) {
	$(modern).parents("tr").find("p").empty().removeAttr("data-id");
}

function creatList(modern,json,level){
	
	$(".listbox").width(150 * (level+1));
	
	if (level == 0 && json) {
		$(".listbox").empty();
		$(modern).find(".listBox").append("<ul></ul>");
		$.each(json, function(index,item) {
			$(".listbox").find("ul").eq(0).append("<li onmouseover='showList(this,1)' onclick='chooseThis(this)' data-id='"+item.id+"' title='"+item.name+"'>"+item.name+"</li>")
		});
        $(".listbox").find("ul").eq(0).append("<li onclick='other(this)' onmouseover='otherActive(this)'>其他</li>")
	}else{
		$(modern).addClass("active").siblings().removeClass("active");
		$(modern).parents(".listBox").find("ul").eq(level-1).nextAll().remove();
		if (json) {
			$(modern).parents(".listBox").append("<ul></ul>");
	    	$.each(json , function(index,item) {
				$(modern).parents(".listBox").find("ul").eq(level).append("<li onmouseover='showList(this,"+(level+1)+")' onclick='chooseThis(this)' data-id='"+item.id+"' title='"+item.name+"' >"+item.name+"</li>")
			});
            $(modern).parents(".listBox").find("ul").eq(level).append("<li onclick='other(this)' onmouseover='otherActive(this)'>其他</li>")
		}
	}
}	

function upData( JSON ) {

	$.ajax({
        url : "",
        data : "",
        success : function ( result ) {
			
        	console.log(result);
        	
        },error : function () {
			
        }
	})
	
}

function otherActive(modern) {
	$(modern).parents("ul").nextAll().remove();
	$(modern).toggleClass("active").siblings().removeClass("active");
}

function other(modern) {
    $(modern).parents("td").find("p").empty().append("<input type='text' onclick='stopEvent()' placeholder='请填写工程名称' / >");
}

function stopEvent() {
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
}

function hideList(modern){
	$(modern).hide();
	window.event ? window.event.cancelBubble = true : e.stopPropagation();
}

function chooseThis(modern){
	var thisId = $(modern).attr("data-id"),
		thisName = $(modern).html();
		
	$(modern).parents("td").find("p").html(thisName).attr({"data-id":thisId , "title" : thisName});
	$(".listBox").hide();
	
	window.event ? window.event.cancelBubble = true : e.stopPropagation();
}

$(document).on("click",function (){
	$(".listBox").hide();
});

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
setInterval(function () {
    saveDate();
},300000);
function  saveDate () {
	var item = $("#mainBox").find("tr");
	var civilTwo = [];
	var costName ;
	for ( i = 1 ; i < item.length ; i++ ){
		if (item.eq(i).find("td").eq(1).find("input").length == 0){
            costName = item.eq(i).find("td").eq(1).find("p").html();
		}else{
            costName = item.eq(i).find("td").eq(1).find("input").val()
		}
		var arr = [
            costName,
            item.eq(i).find("td").eq(2).find("input").val(),
            item.eq(i).find("td").eq(3).find("input").val(),
            item.eq(i).find("td").eq(4).find("input").val(),
            item.eq(i).find("td").eq(5).find("input").val(),
            item.eq(i).find("td").eq(6).find("input").val()

		];
        civilTwo.push(arr);
	}

	var nowUrl = location.href ;

    if (nowUrl.indexOf("industry") != -1) {
        sessionStorage.industryTwo = JSON.stringify(civilTwo);
    }
    if (nowUrl.indexOf("civil") != -1){
        sessionStorage.civilTwo = JSON.stringify(civilTwo);
	}
	if (nowUrl.indexOf("civicism") != -1){
        sessionStorage.civicismTwo = JSON.stringify(civilTwo);
	}
}

function judgeUrl() {
	
}


function getDate() {

    var nowUrl = window.location.href ;

    if (nowUrl.indexOf("industry") != -1 && sessionStorage.industryTwo ) {
		draw("industryTwo")
    }
    if (nowUrl.indexOf("civil") != -1 && sessionStorage.civilTwo ){
        draw("civilTwo")
    }
    if (nowUrl.indexOf("civicism") != -1 && sessionStorage.civicismTwo ){
        draw("civicismTwo")
    }
}

function draw(json) {
    var civilTwo = JSON.parse(sessionStorage.getItem(json));
    var item = $("#mainBox").find("tr");
    for ( var i = 0 , j = 1 ; i < civilTwo.length ; i++ , j++) {
        item.eq(j).find("td").eq(1).find("p").html(civilTwo[i][0]),
            item.eq(j).find("td").eq(2).find("input").val(civilTwo[i][1]),
            item.eq(j).find("td").eq(3).find("input").val(civilTwo[i][2]),
            item.eq(j).find("td").eq(4).find("input").val(civilTwo[i][3]),
            item.eq(j).find("td").eq(5).find("input").val(civilTwo[i][4]),
            item.eq(j).find("td").eq(6).find("input").val(civilTwo[i][5])
    }
}

$(".addTableBtn").on("click",function () {

	var copy = $("#mainBox tr").eq(1).clone(true);
    copy.find("td").eq(0).html($("#mainBox tr").length);
	copy.find(".selectArrow").find("p").empty().removeAttr("data-id","title");
	copy.find("input").val("");

    $("#mainBox").append(copy);

});
